﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test2
{
    //声明委托类型
    delegate void TickHandler(object sender, TimeEventArgs time);
    delegate void AlarmHandler(object sender, TimeEventArgs time);
    class Clock
    {
        private int hour, minute, second;
        public event TickHandler OnTick;
        public event AlarmHandler OnAlarm;
        /**
         * 设置闹钟时间
         */
        public Clock(int hour, int minute)
        {
            if (hour < 60 && minute < 60 && hour >= 0 && minute >= 0)
            {
                this.hour = hour;
                this.minute = minute;
                this.second = 0;
            }
            else
            {
                this.hour = this.minute = this.second = 0;
            }
        }
        public void run()
        {
            while (true)
            {
                if (this.second < 59) this.second++;
                else
                {
                    this.second = 0;
                    this.minute++;
                    if (this.minute == 60)
                    {
                        this.minute = 0;
                        this.hour = (this.hour + 1) % 24;
                    }
                }
                TimeEventArgs CurrentTime = new TimeEventArgs();//当前时间
                if (this.hour == CurrentTime.Hour && this.minute == CurrentTime.Minute)
                {
                    OnAlarm(this, CurrentTime);
                    Console.WriteLine("Clock Stop!");
                    break;
                }
                else
                {
                    OnTick(this, CurrentTime);
                }
                System.Threading.Thread.Sleep(1000);
            }
        }
    }

}
